package Day2025_03_19;

public class LeetCode132 {
    public int minCut(String s) {
        char[] ss = s.toCharArray();
        int n = ss.length;
        boolean[][] dp1 = new boolean[n][n];
        for (int i = n - 1; i >= 0; i--) {
            for (int j = i; j < n; j++) {
                if (ss[i] == ss[j]) dp1[i][j] = i + 1 < j ? dp1[i + 1][j - 1] : true;
            }
        }
        int[] dp2 = new int[n];
        for (int i = 1; i < n; i++) dp2[i] = Integer.MAX_VALUE;
        for (int i = 1; i < n; i++) {
            for (int j = 1; j <= i; j++) {
                if (dp1[0][i]) {
                    dp2[i] = 0;
                    break;
                }
                if (dp1[j][i]) dp2[i] = Math.min(dp2[j - 1] + 1, dp2[i]);
            }
        }
        return dp2[n - 1];
    }
}
